package com.haiyuancloud.base.deal.ctr;

import com.haiyuancloud.base.deal.core.chain.compile.service.*;
import com.haiyuancloud.common.core.utils.SessionDataUtils;
import com.haiyuancloud.db.core.model.DsDataModel;
import com.haiyuancloud.util.ObjectUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Log4j
@RestController
@RequestMapping("/task/ctr")
public class TaskCtr extends BaseCtr {

    @Resource
    private ProductCompileTaskService productCompileTaskService;

    @Resource
    private ProductRunTaskService productRunTaskService;

    /**
     * 编译产品任务
     * @param submitMap  接口参数
     * @return
     */
    @PostMapping("/exportTask")
    public Map<String, Object> exportTask(@RequestBody(required=false) Map<String, Object> submitMap, HttpServletRequest request) {
        if(ObjectUtil.isNull(submitMap)){
            submitMap =  new HashMap<>();
        }
        DsDataModel dsDataModel =  SessionDataUtils.getDsDataModel();
        dsDataModel.setOPEN_PARTITION(dsDataModel.getTENANT_PARTITION());
        Map<String, Object> submitData = this.getDefaultResultData();
        String S_ID = ObjectUtil.toString(submitMap.get("S_ID"));
        productCompileTaskService.compile(dsDataModel,S_ID);
        return submitData;
    }
    /**
     * 发布产品任务
     * @param submitMap  接口参数
     * @return
     */
    @PostMapping("/deployTask")
    public Map<String, Object> deployTask(@RequestBody(required=false) Map<String, Object> submitMap, HttpServletRequest request) {
        if(ObjectUtil.isNull(submitMap)){
            submitMap =  new HashMap<>();
        }
        DsDataModel dsDataModel =  SessionDataUtils.getDsDataModel();
        dsDataModel.setOPEN_PARTITION(dsDataModel.getTENANT_PARTITION());
        Map<String, Object> submitData = this.getDefaultResultData();
        productRunTaskService.run(dsDataModel,submitMap);
        return submitData;
    }
}
